#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
// 从键盘读取数据，写⼊管道，读取管道，写到屏幕

int main()
{
    int fd[2];
    char buffer[100];
    int len = 0;
    if (pipe(fd) == -1)
    {
        perror("pipe is error\n");
        return 1;
    }

    while (fgets(buffer, 100, stdin))
    {
        len = strlen(buffer);
        // 把数据写到管道中
        if (write(fd[1], buffer, len) != len)
        {
            perror("write is false\n");
            break;
        }
        // 将字符数组重置
        memset(buffer, 0x00, sizeof(buffer));

        // 管道数据读取
       
        //注意这里的括号不能省去，不然len的值会一直被赋值为0
        //是后面的判断先进行判断，判断后的值会赋值给len
        if ((len = read(fd[0], buffer, 100)) == -1)
        {
            perror("read is false\n");
            break;
        }

        if (write(1, buffer, len) == -1)
        {
            perror("write is error\n");
            break;
        }
        fflush(stdout);
    }

    return 0;
}